umpythonfandomcom-20200213-history
Frequently Asked Questions
'How do I convert and integer to a string and vice versa?' You can use the int() function to convert the string to an int: x = "555" y = int(x) y += 5 #y now equals 560 You can use the str() function to convert an int to a string: x = 2012 y = str(x) # y now equals the string "2012" 'How do I create my own method/function in Python?' To create a function in Python use the the def keyword: #This method takes a string as a parameter, prints that string, #then adds a "!" to the end of the given string. def printMessage(x): print "This is the print method..." print x return x + "!" #Later on... x = "Hello World" x = printMessage(x) print x 'How do I create an array in Python?' To create an array in Python use the [] brackets: list = 1,2,3,4,5,6,7,8,9,10 x = list2 #x now equals 3 'How do I read input from the keyboard?' To get text input from the keyboard use the raw_input() function: theInput = raw_input() print theInput # OR theInput = raw_input("Enter some text") print theInput Notice that you can use raw_input() with or without an argument. The second example provides an argument which prompts the user to type something by printing a message to the screen. Another thing to remember is that the input that is stored in the variable theInput is in the form of a string. If it is intended to be used as a number you must convert it first. 'How do I sort a list?' There is more than one way to sort a list in Python. You can do it manually by using a loop and switching the items or you can use the sort() function: #Create a list and sort it in ascending order theList = 8,2,36,7,4,9 theList.sort() print theList #This will print 2,4,7,8,9,36 'How should I choose variable, function, and class names?' Variable and function names should be all lower case with underscores to separate the words. Function names should start with a verb and be descriptive so that it is easy to understand what it does. Class names should use capital letters to start each word without using underscores. When declaring constants in Python you should use all caps. Using similar naming styles helps programmers read and interpret code much faster. 'How do I incrment numbers in Python?' In most programming languages you can use the ++ operator to increment numbers. This feature is not available in Python but is replaced with +=1. #Increment an integer the_count = 0; the_count += 1; #the_count now equals 1 'How do I read from a text file?' To read text from a file you must use the open() function. Open() takes 2 strings as parameters. The first parameter is the file path of the text file to be read and the second parameter is a string specifying whether you intend to read from the file or write to it. #Open and read a file on the desktop the_file = open("/home/billy/Desktop/atextfile.txt","r") #or "w" for writing data = readline() #gets 1 line of the file data = read() #gets entire file Note that if you use the readline() function you will have to use a while loop to read the entire file. 'What does it mean when you say Python is interpreted instead of compiled?' In general there are two types of programming languages, interpreted and compiled. With a compiled language, the code you write is converted into machine code that is specific to your type of computer. The compiler goes through your program line by line and converts each instruction into a machine level instruction and produces an executable out of that code. Therefore to run a compiled program you use two steps, first compile the program then run the executable. An interpreted language uses an interpreter program which reads the code, translates it to computations and system calls, and executes it in a line by line fashion. This means that each time the program is run, it has to be read and translated which points out the main difference of the two types of programming languages. The difference is that an interpreted language is generally slower because it has to read and translate the code each time it is run where as with a compiled language you produce an executable which will not need to be converted again.